package org.cid54.jshopper.dao.impl;

import java.util.List;
import org.cid54.jshopper.dao.ShoppingListDao;
import org.cid54.jshopper.model.ShoppingList;
import org.cid54.jshopper.model.UserGroup;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 *
 * @author fgo
 */
@Repository
public class ShoppingListDaoImpl extends SuperEntityDaoImpl<ShoppingList> implements ShoppingListDao {

    @Override
    public List<ShoppingList> findByUserGroup(UserGroup userGroup) {
        Criteria criteria = this.getNewCriteria();

        criteria.add(Restrictions.eq("userGroup", userGroup));

        criteria.setFetchMode("shopItems", FetchMode.JOIN);

        return criteria.list();
    }

    @Override
    protected Class getEntityClass() {
        return ShoppingList.class;
    }

    @Override
    public ShoppingList findByIdWithShopItems(long shoppingListId) {
        Criteria criteria = this.getNewCriteria();

        criteria.add(Restrictions.eq("id", shoppingListId));

        criteria.setFetchMode("shopItems", FetchMode.JOIN);

        return (ShoppingList) criteria.uniqueResult();
    }

    @Override
    public List<ShoppingList> findByUserGroups(List<UserGroup> userGroups) {
        Criteria criteria = this.getNewCriteria();

        criteria.add(Restrictions.in("userGroup", userGroups));

        return criteria.list();
    }

    @Override
    public ShoppingList findByName(String name) {
        Criteria criteria = this.getNewCriteria();

        criteria.add(Restrictions.eq("name", name));

        return (ShoppingList) criteria.uniqueResult();
    }

    @Override
    protected boolean isSortableByName() {
        return true;
    }
}
